﻿using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web.UI;
using System;

namespace OF.Controls
{
    /// <summary>
    /// 文件名称：搜索控件接口
    /// </summary>
    public interface ISearcher
    {
        /// <summary>
        /// 获取或设置sql条件字段
        /// </summary>
        string SqlField { get; set; }
        /// <summary>
        /// 获取或设置sql条件关系符
        /// </summary>
        string SqlCharacter { get; set; }
        /// <summary>
        /// 获取搜索条件语句
        /// </summary>
        /// <returns></returns>
        string GetSqlWhere();
        /// <summary>
        /// 获取当前控件的ID
        /// </summary>
        /// <returns></returns>
        string GetControlId();
        /// <summary>
        /// 获取或设置搜索分组
        /// </summary>
        string SearcherGroupName { get; set; }
    }
    /// <summary>
    /// 文件名称：搜索控件集合
    /// </summary>
    public class SearcherColletion : IEnumerable<ISearcher>
    {
        protected List<ISearcher> lstSearchers = new List<ISearcher>();

        private readonly Control contain;

        /// <summary>
        /// 从Page对象或容器控件对象实例化一个搜索控件集合
        /// </summary>
        /// <param name="contain"></param>
        public SearcherColletion(Control contain)
        {
            this.contain = contain;
        }

        private void LoadSearchers(string searcherGroupName = "")
        {
            lstSearchers.Clear();

            if (contain.HasControls())
            {
                GetControls(contain, searcherGroupName);
            }
        }
        /// <summary>
        /// 递归加载搜索控件
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="groupName"></param>
        private void GetControls(Control parent, string groupName = "")
        {
            ControlCollection controls = parent.Controls;
            foreach (Control ctl in controls)
            {
                if (ctl is ISearcher)
                {
                    var searcher = ctl as ISearcher;
                    if (string.IsNullOrWhiteSpace(groupName))
                    {
                        lstSearchers.Add(searcher);
                    }
                    else if (searcher.SearcherGroupName == groupName)
                    {
                        lstSearchers.Add(searcher);
                    }
                }
                else if (ctl.HasControls())
                {
                    GetControls(ctl, groupName);
                }
            }
        }
        /// <summary>
        /// 返回搜索控件集合的组合搜索条件的sql where语句表示
        /// </summary>
        /// <returns></returns>
        public string GetSqlWhere(string searcherGroupName = "")
        {
            LoadSearchers(searcherGroupName);
            return GetSql();
        }
        /// <summary>
        /// 获取搜索控件集合的组合搜索条件
        /// </summary>
        /// <returns></returns>
        private string GetSql()
        {
            var strWhere = lstSearchers.Select(searcher => searcher.GetSqlWhere()).Where(strItem => !string.IsNullOrWhiteSpace(strItem)).Aggregate(string.Empty, (current, strItem) => current + (" and " + strItem));
            if (strWhere.Length > 4)
            {
                strWhere = strWhere.Substring(4);
            }
            return strWhere;
        }
        /// <summary>
        /// 返回一个循环访问集合的枚举器。
        /// </summary>
        /// <returns>可用于循环访问集合的 System.Collections.Generic.IEnumerator。</returns>
        public IEnumerator<ISearcher> GetEnumerator()
        {
            return ((IEnumerable<ISearcher>)lstSearchers).GetEnumerator();
        }

        /// <summary>
        /// 返回一个循环访问集合的枚举器。
        /// </summary>
        /// <returns>可用于循环访问集合的 System.Collections.Generic.IEnumerator。</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// 根据分页控件记住的搜索条件初始化页面搜索控件的值，并返回启用的搜索控件所在的搜索分组名称列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="searcherGroupName"></param>
        /// <returns></returns>
        public List<string> SetPage(string strWhere, string searcherGroupName = "")
        {
            List<string> lstGroups = new List<string>();
            LoadSearchers(searcherGroupName);
            string[] wheres = strWhere.ToLower()
                                      .Replace(" and ", "&")
                                      .Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            int indexS = 0, indexW = 0;

            do
            {
                string[] nv = wheres[indexW].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                ISearcher searcher = lstSearchers[indexS];
                if (nv.Length == 3)
                {
                    if (nv[0] == searcher.SqlField.ToLower())
                    {
                        SetValue(searcher, nv[2].Trim(@"'()%".ToCharArray()));
                        //SetControlValue(searcher, nv);
                        if (!lstGroups.Contains(searcher.SearcherGroupName))
                        {
                            lstGroups.Add(searcher.SearcherGroupName);
                        }
                        indexS++;
                        indexW++;
                    }
                    else
                    {
                        indexS++;
                    }
                }
            } while (indexS < lstSearchers.Count && indexW < wheres.Length);
            return lstGroups;
        }

        private void SetValue(ISearcher searcher, string value)
        {
            if (searcher is OFTextBox) (searcher as OFTextBox).Text = value;
            else if (searcher is OFDropDownList)
                (searcher as OFDropDownList).SelectedIndex =
                    (searcher as OFDropDownList).Items.IndexOf((searcher as OFDropDownList).Items.FindByValue(value));
            else if (searcher is OFCheckBox) (searcher as OFCheckBox).Checked = value == "1";

        }
    }
}
